package repo

import (
	"encoding/base64"
	"time"
)

type TsInfo struct {
	Xid       string    `json:"xid"`       // transaction XID
	PXid      string    `json:"pXid"`      // transaction parent XID
	PrtAddr   string    `json:"prtAddr"`   // address of participant transaction
	SrvNm     string    `json:"srvNm"`     // service name of participant transaction
	Param     string    `json:"param"`     // parameter data
	StTm      time.Time `json:"stTm"`      // start time
	CliSDKVer string    `json:"cliSdkVer"` // version of client SDK
	Env       string    `json:"env"`       // environment
	Wks       string    `json:"wks"`       // workspace
	Hder      string    `json:"hder"`      // headers
}

func (t *TsInfo) encode() (string, error) {
	if len(t.Hder) > 0 {
		hderStr := base64.StdEncoding.EncodeToString([]byte(t.Hder))
		t.Hder = hderStr
	}
	return json.MarshalToString(t)
}

func (t *TsInfo) decode(bytes []byte) error {
	if err := json.Unmarshal(bytes, t); err != nil {
		return err
	}
	if len(t.Hder) > 0 {
		str, err := base64.StdEncoding.DecodeString(t.Hder)
		if err != nil {
			return err
		}
		t.Hder = string(str)
	}
	return nil
}
